import api from '@/api'
import consts from '@/const'
import { deepCopy, setPage } from '@/utils/util'

const initPage = {
    size: 10,
    number: 0,
    total: 0,
    pages: 0,
    filter: {
        enabled: null,
        classIds: null,
        name: null
    }
}

const state = {
    data: {
        evidenClasses: [],
        evidences: [],
        currentEvidence: null,
        evidenceImages: [],
        materialImages: []
    },
    ui: {
        page: deepCopy(initPage)
    }
}

// getters
const getters = {
    evidenClasses: state => state.data.evidenClasses,
    evidences: state => state.data.evidences,
    currentEvidence: state => state.data.currentEvidence,
    evidenceImages: state => state.data.evidenceImages,
    materialImages: state => state.data.materialImages,
    evidenTotal: state => state.ui.page.total
}

// actions
const actions = {
    getEvidenceClasses({ commit }) {
        api.getClassifications('key_class_evidence', {
            loading: true,
            success: evidenClasses => {
                commit('setEvidenceClasses', { evidenClasses })
            }
        })
    },
    saveEvidenceClass({ dispatch }, data) {
        api.saveClassification(
            { ...data, classType: 'key_class_evidence' },
            {
                success: () => {
                    dispatch('getEvidenceClasses')
                }
            }
        )
    },
    removeEvidenceClass({ dispatch }, id) {
        api.removeClassification(id, {
            success: () => {
                dispatch('getEvidenceClasses')
            }
        })
    },
    getEvidence({ commit }, { id, cb }) {
        api.getEvidence(id, {
            success: currentEvidence => {
                commit('setCurrentEvidence', { currentEvidence })
                cb && cb(currentEvidence)
            }
        })
    },
    getEvidences({ commit, state }, page) {
        api.getEvidences(page ? page : state.ui.page, {
            loading: true,
            success: res => {
                const { pages, total } = res
                console.log('getEvidences', pages, total)
                commit('setEvidenPage', { pages, total })
                commit('setEvidences', res)
            }
        })
    },
    saveEvidence({ dispatch }, data) {
        api.saveEvidence(data, {
            success: () => {
                dispatch('getEvidences')
            }
        })
    },
    removeEvidences({ dispatch }, ids) {
        api.removeEvidences(ids, {
            success: () => {
                dispatch('getEvidences')
            }
        })
    },
    getEvidenceImages({ commit }, ownerId) {
        const { evidence } = consts
        const purpose = evidence.FILE_EVID
        api.getMedias(
            'image',
            { filter: { purpose, ownerId } },
            {
                loading: true,
                success: res => {
                    commit('setEvidenceImages', { evidenceImages: res.content })
                }
            }
        )
    },
    removeEvidenceImage({ dispatch }, { fileId, ownerId }) {
        api.removeMedia('image', fileId, {
            success: () => {
                dispatch('getEvidenceImages', ownerId)
            }
        })
    },
    removeIcon({ dispatch }, id) {
        api.removeMedia('image', id, {
            success: () => {}
        })
    },
    getMaterialImages({ commit }, { classIds }) {
        const strategy = 'simple'
        const type = 'image'
        api.getMedias(
            type,
            { strategy, filter: { classIds } },
            {
                success: res => {
                    commit('setMaterialImages', res)
                }
            }
        )
    }
}

// mutations
const mutations = {
    setEvidenceClasses: (state, payload) => {
        state.data.evidenClasses = payload.evidenClasses
    },
    setEvidenPage: (state, page) => {
        page && setPage(state.ui.page, page)
    },
    setCurrentEvidence: (state, payload) => {
        state.data.currentEvidence = payload.currentEvidence
    },
    setEvidences: (state, payload) => {
        state.data.evidences = payload.content
    },
    setEvidenceImages: (state, payload) => {
        state.data.evidenceImages = payload.evidenceImages ? payload.evidenceImages.reverse() : []
    },
    setMaterialImages: (state, payload) => {
        state.data.materialImages = payload.content
    }
}

export default {
    state,
    getters,
    actions,
    mutations
}
